package org.vergil.core.guava.Thread;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by vergil on 4/23/17.
 */
public class GetThread implements Runnable {

    private EventBus eventBus;

    private BlockingDeque<Integer> deque = new LinkedBlockingDeque<>();

    final static Logger log = LoggerFactory.getLogger(GetThread.class);

    public GetThread(EventBus eventBus){
        this.eventBus = eventBus;
        this.eventBus.register(this);
    }
    @Subscribe
    public void handle(Integer num) throws InterruptedException {
        deque.put(num);
    }

    @Override
    public void run() {
        while (true){
            try {
                log.info("{} get num is {}", Thread.currentThread().getName(), deque.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
